
sub my_read_opts {
    my($ref_parms, $ref_order, $ref_cats, $ref_catorder, $config_file, $debug) = @_;
    my($key, $value, $value_continued, $help, $help_flag);
                                # If debug == 0 (instead of undef) this is disabled
    print "Reading config_file $config_file\n" unless defined $debug and $debug == 0;
    open (CONFIG, "$config_file") or print "\nError, could not read config file: $config_file\n";
    my $category = "Other";
    while (<CONFIG>) {

                                # Look for help text
        if (/^\@(.*)/) {
            $help = '' unless $help_flag++;
            $help .= $1 . "\n";
            next;
        }
        else {
            $help_flag = 0;
        }
        if (/ \@(.+)/) {
            $help = $1;
        }

        if ($_ =~ /^#\s*Category\s*=\s*(.*?)\s*$/) {
            $category = $1;
            push @$ref_catorder, $category if ! grep /^$category$/, @$ref_catorder;
        }

        next if /^\s*\#/;
                                # Allow for multi-line values records
        if ($key and ($value) = $_ =~ /^\s+([^\#]+)/ and $value !~ /=/) {
            $value_continued = 1;
        }
                                # Look for normal key=value records
        else {
            next unless ($key, $value) = $_ =~ /(\S+?)\s*=\s*(.*)/;
            if ($value) {
                $value =~ s/^[\#\@].*//; # Delete end of line comments
                $value =~ s/\s+[\#\@].*//;
            }
            $value_continued = 0;
            next unless $key;
        }

        $value =~ s/\s+$//;     # Delete end of value blanks

                                # Last parm wins (in case we reload parm file)
        if ($value_continued) {
            $$ref_parms{$key} .= $value;
        }
        else {
            $help{$key} = $help if $help;
            $$ref_parms{$key}  = $value;
            if (! grep /^$key$/, @$ref_order) {
                push @$ref_order, $key;
                $$ref_cats{$key} = $category;
            }
        }
        print "parm key=$key value=$$ref_parms{$key} category=$$ref_cats{$key}\n" if $debug;
    }
    close CONFIG;
    return sort keys %{$ref_parms};
}

